﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.COM;
using System.Collections;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class M009_STORAGE_INPUT : FormBase
    {
        #region 变量定义

        XGWMS.BLL.BLL_M009_STORAGE_INPUT bll_M009_STORAGE_INPUT = new XiaoGang.Soft.XGWMS.BLL.BLL_M009_STORAGE_INPUT();
        XGWMS.BLL.BLL_M007_GOODS bllGoods = new XiaoGang.Soft.XGWMS.BLL.BLL_M007_GOODS();
        public static string strGoodID = "";//商品编号
        public static string strGoodNM = "";//商品名称
        bool blFormLoad = false;
        private string strGroupTitle = "入库单信息";
        bool isEnter = false;
        private const string dataName = "入库";

        #endregion

        #region 方法

        public M009_STORAGE_INPUT()
        {
            InitializeComponent();
        }

        /// <summary>
        /// <p><b>方法名称</b>SetTabIndex</p>	
        /// <p><b>概要説明	:</b>画面フォーカスの遷移順の設定</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// </summary>
        private void SetTabIndex()
        {
            int intIndex = 0;
            grpTempBillInfo.TabIndex = intIndex; intIndex++;
            txtGOODS_NM.TabIndex = intIndex; intIndex++;
            btnGoodsIDSearch.TabIndex = intIndex; intIndex++;
            txtBUSINESS_TIME.TabIndex = intIndex; intIndex++;
            cmbSTOREROOM_ID.TabIndex = intIndex; intIndex++;
            btnAdd.TabIndex = intIndex; intIndex++;
            txtGOODS_No.TabIndex = intIndex; intIndex++;
        }

        /// <summary>
        /// <p><b>方法名称</b>SetEnable</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>概要説明  :</b>画面モードのセト</p>
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        private void SetEnable()
        {
            txtGOODS_No.Enabled = true;
            btnGoodsIDSearch.Enabled = true;
            txtGOODS_NM.Enabled = true;      
            txtBUSINESS_TIME.Enabled = true;
            cmbSTOREROOM_ID.Enabled = true;
            dtpBUSINESS_TIME.Enabled = true;
            grpTempBillInfo.Enabled = true;
            grdmentTempBillInfo.Enabled = true;
            btnAdd.Enabled = true;
        }

        /// <summary>
        /// <p><b>方法名称</b>ClearControl</p>	
        /// <p><b>概要説明	:</b>画面上の全項目をオールクリアする</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// </summary>
        private void ClearControl()
        {
            txtGOODS_No.Text = "";
            txtGOODS_NM.Text = "";
            cmbSTOREROOM_ID.SelectedIndex = 0;
            txtBUSINESS_TIME.Text = DateTime.Now.ToString("yyyyMMdd").Insert(4, "/").Insert(7, "/");
            dtpBUSINESS_TIME.Text = "";
        }

        /// <summary>
        /// <p><b>方法名称</b>InitComBox</p>	
        /// <p><b>概要説明	:</b>ComBoxの初期化する</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// </summary>
        private void InitComBox()
        {
            Method.CmbDataBound("STOREROOM_ID", cmbSTOREROOM_ID);           
            cmbSTOREROOM_ID.SelectedIndex = 0;    
        }

        /// <summary>
        /// <p><b>方法名称</b>InitialGridView</p>	
        /// <p><b>概要説明	:</b>Grid初始化</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// </summary>
        private void InitialGridView()
        {
            grdmentTempBillInfo.ReadOnly = false;
            grdmentTempBillInfo.ImeMode = ImeMode.Disable;
            grdmentTempBillInfo.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            grdmentTempBillInfo.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
            grdmentTempBillInfo.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            grdmentTempBillInfo.ColumnHeadersHeight = 30;
            grdmentTempBillInfo.ColumnHeadersVisible = true;
            grdmentTempBillInfo.AllowUserToResizeColumns = true;
            grdmentTempBillInfo.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            grdmentTempBillInfo.Columns.Add("NO.", "NO.");
            grdmentTempBillInfo.Columns.Add("GOODS_ID", "商品编码");
            grdmentTempBillInfo.Columns.Add("GOODS_NM", "商品名称");
            grdmentTempBillInfo.Columns.Add("PRODUCE_DATE", "生产日期");
            grdmentTempBillInfo.Columns.Add("OVER_PRODUCE_DATE", "过期日期");
            grdmentTempBillInfo.Columns.Add("BUYING_PRICE", "进货价");
            grdmentTempBillInfo.Columns.Add("UNITS", "数量");
            DataGridViewButtonColumn btnDelete = new DataGridViewButtonColumn();
            btnDelete.Text = "删除";
            btnDelete.Width = 80;
            btnDelete.HeaderText = "删除";
            grdmentTempBillInfo.Columns.Add(btnDelete);
            DataGridViewCheckBoxColumn chkRed_Black = new DataGridViewCheckBoxColumn();
            chkRed_Black.Width = 50;
            chkRed_Black.HeaderText = "冲账";
            chkRed_Black.Name = "Red_Black";
            grdmentTempBillInfo.Columns.Add(chkRed_Black);
            grdmentTempBillInfo.Columns.Add("MEMO", "备注");

            grdmentTempBillInfo.Columns["NO."].Width = 50;
            grdmentTempBillInfo.Columns["NO."].Frozen = true;
            grdmentTempBillInfo.Columns["NO."].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentTempBillInfo.Columns["NO."].ReadOnly = true;
            grdmentTempBillInfo.Columns["NO."].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentTempBillInfo.Columns["GOODS_ID"].Width = 120;
            grdmentTempBillInfo.Columns["GOODS_ID"].ReadOnly = true;
            grdmentTempBillInfo.Columns["GOODS_ID"].Resizable = DataGridViewTriState.False;
            grdmentTempBillInfo.Columns["GOODS_ID"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentTempBillInfo.Columns["GOODS_NM"].Width = 240;
            grdmentTempBillInfo.Columns["GOODS_NM"].ReadOnly = true;
            grdmentTempBillInfo.Columns["GOODS_NM"].Frozen = true;
            grdmentTempBillInfo.Columns["GOODS_NM"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentTempBillInfo.Columns["PRODUCE_DATE"].Width = 70;
            grdmentTempBillInfo.Columns["PRODUCE_DATE"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentTempBillInfo.Columns["PRODUCE_DATE"].ReadOnly = false;
            grdmentTempBillInfo.Columns["PRODUCE_DATE"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentTempBillInfo.Columns["OVER_PRODUCE_DATE"].Width = 70;
            grdmentTempBillInfo.Columns["OVER_PRODUCE_DATE"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentTempBillInfo.Columns["OVER_PRODUCE_DATE"].ReadOnly = false;
            grdmentTempBillInfo.Columns["OVER_PRODUCE_DATE"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentTempBillInfo.Columns["UNITS"].Width = 60;
            grdmentTempBillInfo.Columns["UNITS"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdmentTempBillInfo.Columns["UNITS"].ReadOnly = false;
            grdmentTempBillInfo.Columns["BUYING_PRICE"].Width = 80;
            grdmentTempBillInfo.Columns["BUYING_PRICE"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdmentTempBillInfo.Columns["BUYING_PRICE"].ReadOnly = false;
            grdmentTempBillInfo.Columns["MEMO"].Width = 126;
            grdmentTempBillInfo.Columns["MEMO"].ReadOnly = false;

            grdmentTempBillInfo.AllowUserToAddRows = false;
            grdmentTempBillInfo.AllowUserToDeleteRows = false;
            grdmentTempBillInfo.AllowUserToResizeRows = false;
            grdmentTempBillInfo.RowHeadersVisible = false;
            grdmentTempBillInfo.RowTemplate.Height = 20;
            grdmentTempBillInfo.RowTemplate.ReadOnly = false;
            grdmentTempBillInfo.RowTemplate.Resizable = DataGridViewTriState.False;
        }

        /// <summary>
        /// <p><b>方法名称</b>InitProcess</p>	
        /// <p><b>概要説明	:</b>窗口初始化</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// </summary>
        private void InitProcess()
        {
            SetTabIndex();
            SetEnable();
            InitialGridView();
            InitComBox();
            ClearControl();
        }

        /// <summary>	
        /// <p><b>方法名称</b>GetGoodsName</p>	
        /// <p><b>概要説明  :</b>获取商品的名称</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/16</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        /// <param name="strValue">商品ID</param> 
        /// <returns type="string">商品名称</returns>
        protected bool GetGoodsName(string strValue)
        {
            XiaoGang.Soft.XGWMS.BLL.BLL_M007_GOODS bll_M007_GOODS =
                new XiaoGang.Soft.XGWMS.BLL.BLL_M007_GOODS();
            MODEL.MOD_M007_GOODS model =
                new XiaoGang.Soft.XGWMS.MODEL.MOD_M007_GOODS();
            model.Clear();
            model.GOODS_ID = strValue.Trim();

            ReturnValue rvReturnValue = bll_M007_GOODS.GetSingleData(model);

            if (rvReturnValue.IsSuccess == false)
            {
                Method.MessageShow(rvReturnValue.ErrorID,dataName);
                return false;
            }
            if (rvReturnValue.ResultDataSet.Tables[0].Rows.Count == 0)
            {
                Method.MessageShow("W11008",dataName);
                return false;
            }

            txtGOODS_NM.Text =
                rvReturnValue.ResultDataSet.Tables[0].Rows[0]["GOODS_NM1"].ToString().Trim();
            return true;
        }

        /// <summary>
        /// <p><b>方法名称</b>CheckProcess</p>	
        /// <p><b>概要説明  :</b>画面のデータのＣＨＥＣＫ</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="string" param="strConstrolName">控件名称</param>	
        /// <returns type="Boolean">　True:成功   False:失敗 </returns>
        protected override bool CheckProcess(string strConstrolName)
        {
            switch (strConstrolName)
            {
                case "txtGOODS_ID":
                    if (Method.CheckIsNumber(txtGOODS_No.Text.Trim(),txtGOODS_No.MaxLength,true,true) == false)
                    {
                        Method.MessageShow( GlobalVal.glostrMsg,dataName);
                        txtGOODS_No.Focus();
                        return false;
                    }
                    //if (GetGoodsName(txtGOODS_ID.Text.Trim()) == false)
                    //{
                    //    txtGOODS_ID.Focus();
                    //    return false;
                    //}                    
                    break;
                case "txtGOODS_NM":
                    if (0 == txtGOODS_NM.Text.Trim().Length)
                    {
                        Method.MessageShow("W11E02", "商品名称");//[@]不能为空！
                        txtGOODS_NM.Focus();
                        return false;
                    }
                    if (Method.CheckIsIncludeSpecailChar(txtGOODS_NM.Text.Trim()) == false)
                    {
                        Method.MessageShow(GlobalVal.glostrMsg,dataName);
                        txtGOODS_NM.Focus();
                        return false;
                    }
                    if (txtGOODS_NM.Text.Trim().Length > txtGOODS_NM.MaxLength)
                    {
                        Method.MessageShow("W11007",dataName);
                        txtGOODS_NM.Focus();
                        return false;
                    }
                    break;
                case "txtBUSINESS_TIME":
                    if (CheckProcess(txtBUSINESS_TIME.Text.Trim().Replace("/",""), enumCheckItems.DateYMD,false, null) == false)
                    {
                        txtBUSINESS_TIME.Focus();
                        return false;
                    }
                    break;
            }

            return true;
        }

        /// <summary>	
        /// <p><b>方法名称</b>CheckProcessLoop</p>	
        /// <p><b>概要説明  :</b>画面のデータのＣＨＥＣＫ</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="string" param="strConstrolName">控件名称</param>	
        /// <returns type="Boolean">　True:成功   False:失敗 </returns>
        private bool CheckProcessLoop(enumOperation emOperation)
        {
            switch (emOperation)
            {   
                case enumOperation.Add:
                    if (CheckProcess("txtBUSINESS_TIME") == false) return false;
                    break;
            }
            return true;
        }

        /// <summary>	
        /// <p><b>方法名称</b>NumPriceFormat</p>	
        /// <p><b>概要説明  :</b>num and price date's format</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="string" param="strValue">没有格式化的字符串</param>	
        /// <returns type="string">格式化后的字符串</returns>
        protected string NumPriceFormat(string strValue)
        {
            string strTemp = strValue;
            if (strValue.Length > 8)
            {
                return strValue;
            }
            if (strValue.Length > 6)
            {
                strTemp = strValue.Substring(0, strValue.Length - 6) + ","
                    + strValue.Substring((strValue.Substring(0, strValue.Length - 6)).Length, 3)
                    + "," + strValue.Substring(strValue.Length - 3, 3);

            }
            else if (strValue.Length > 3)
            {
                strTemp = strValue.Substring(0, strValue.Length - 3) + ","
                + strValue.Substring((strValue.Substring(0, strValue.Length - 3)).Length, 3);
            }
            return strTemp;
        }

        /// <summary>	
        /// <p><b>方法名称</b>DateFormat</p>	
        /// <p><b>概要説明  :</b>date data format</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/16</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        /// <param name="strValue">没有格式化的字符串</param> 
        /// <returns type="string">格式化后的字符串</returns>
        protected string DateFormat(string strValue)
        {
            if (strValue.Length < 8)
            {
                return strValue;
            }
            return strValue.Substring(0, 4) + "/" +
                strValue.Substring(4, 2) + "/" +
                strValue.Substring(6, 2);
        }

        /// <summary>	
        /// <p><b>方法名称</b>AddRowToGird</p>	
        /// <p><b>概要説明  :</b>a new row add to grid</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        private void AddRowToGird(string strGoodID, string strGoodNM)
        {
            //首先查找入库单中是否已经包含本商品
            for (int i = 0; i < grdmentTempBillInfo.Rows.Count; i++)
            {
                //有则修改数量
                if (strGoodID.Trim() == grdmentTempBillInfo.Rows[i].Cells["GOODS_ID"].Value.ToString().Trim())
                {
                    grdmentTempBillInfo.Rows[i].Cells["GOODS_ID"].Value = strGoodID;
                    grdmentTempBillInfo.Rows[i].Cells["GOODS_NM"].Value = strGoodNM;
                    int intTempUnits = 0;
                    try
                    {
                        intTempUnits=Int32.Parse(grdmentTempBillInfo.Rows[i].Cells["UNITS"].Value.ToString().Trim());
                    }
                    catch { }
                    grdmentTempBillInfo.Rows[i].Cells["UNITS"].Value = intTempUnits + 1;
                    //grdmentTempBillInfo.CurrentCell = grdmentTempBillInfo.Rows[i].Cells["UNITS"];
                    return;
                }
            }
            //没有则进行添加
            //grdmentTempBillInfo.Columns.Add("MEMO", "备注");

            grdmentTempBillInfo.Rows.Insert(grdmentTempBillInfo.Rows.Count,
                (grdmentTempBillInfo.Rows.Count + 1).ToString(),//NO.
                strGoodID,//商品编码
                strGoodNM,//商品名称
                "",//生产日期
                "",//过期日期
                "",//进货价
                1,//数量
                "删除",//删除
                false,//冲账
                ""//备注
                );
            //grdmentTempBillInfo.CurrentCell = grdmentTempBillInfo.Rows[grdmentTempBillInfo.Rows.Count].Cells["UNITS"];
            grpTempBillInfo.Text = strGroupTitle + "  共" + grdmentTempBillInfo.Rows.Count.ToString() + "件";
            if (grdmentTempBillInfo.Rows.Count > 16)
            {
                grdmentTempBillInfo.Columns["GOODS_NM"].Width = 223;
            }
            else
            {
                grdmentTempBillInfo.Columns["GOODS_NM"].Width = 240;
            }
        }

        /// <summary>
        /// 商品入库
        /// </summary>
        /// <param name="modelSTORAGE"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        public bool InPutStorage(List<MODEL.MOD_M009_STORAGE_INPUT> modelSTORAGE, out string strMessage)
        {

            #region 创建新的货单号

            string strReturn = string.Empty;
            MODEL.MOD_M010_BILL modelBill = new XiaoGang.Soft.XGWMS.MODEL.MOD_M010_BILL();
            modelBill.UPDATER_ID = GlobalVal.gloUserInfo.USER_ID.Trim();
            modelBill.TERMINAL_CD = GlobalVal.gloUserInfo.TERMINAL_CD.Trim();
            string strBillID = bll_M009_STORAGE_INPUT.GetBillID(modelBill);
            if (strBillID.Trim().Length != 12)
            {
                strMessage = strBillID.Trim();
                return false;
            }

            #endregion

            #region INSERT INTO BILL

            modelBill.TRANS_STATE = "1";
            modelBill.BILL_ID = strBillID;
            modelBill.IN_OUT = "0";
            modelBill.BUSINESS_TIME = txtBUSINESS_TIME.Text.Trim().Replace("/", "");
            modelBill.MEMO = "正常入库";
            modelBill.USER_NO = "";
            strReturn = bll_M009_STORAGE_INPUT.InsertProcess(modelBill);
            if (strReturn.Trim() != "0")
            {
                strMessage = strReturn.Trim();
                return false;
            }

            #endregion

            #region InTo Storage
            XGWMS.MODEL.MOD_M007_GOODS modGoods = new XiaoGang.Soft.XGWMS.MODEL.MOD_M007_GOODS();
            modGoods.UPDATER_ID = GlobalVal.gloUserInfo.USER_ID.Trim();
            modGoods.TERMINAL_CD = GlobalVal.gloUserInfo.TERMINAL_CD.Trim();
            ReturnValue retuenValue;
            for (int i = 0; i < modelSTORAGE.Count; i++)
            {
                modelSTORAGE[i].BILL_ID = strBillID;
                strReturn = bll_M009_STORAGE_INPUT.InsertProcess(modelSTORAGE[i]);
                //Message
                if (strReturn.Trim() != "0")
                {
                    strMessage = strReturn.Trim();
                    return false;
                }

                #region 更改库存商品数量

                modGoods.UNITS = modelSTORAGE[i].UNITS;
                modGoods.GOODS_ID = modelSTORAGE[i].GOODS_ID;
                retuenValue = bllGoods.AddGoodsUnits(modGoods);
                if (!retuenValue.IsSuccess)
                {
                    strMessage=retuenValue.ErrorID;
                    return false;
                }

                #endregion
            }

            #endregion

            strMessage = "";
            return true;
        }

        /// <summary>
        /// 商品检索
        /// </summary>
        private void SearchGoods()
        {
            if (GlobalVal.gloSearchGoodsForm == null)
            {
                GlobalVal.gloSearchGoodsForm = new M007_GOODS(true);
            }
            GlobalVal.gloSearchGoodsForm.ShowSearchGoodsDialog(this, "", txtGOODS_No.Text.Trim(), txtGOODS_NM.Text.Trim());
            if (GlobalVal.gloSelectedGoods.ItemCount > 0)
            {
                AddRowToGird(GlobalVal.gloSelectedGoods.SelectedItems[0].GOODS_ID
                    , GlobalVal.gloSelectedGoods.SelectedItems[0].GOODS_NM1);
            }

            //strGoodID = "";//商品编号
            //strGoodNM = "";//商品名称
            //M007_GOODS frm_M007_GOODS = new M007_GOODS(true, "M009_STORAGE_INPUT");
            //frm_M007_GOODS.StartPosition = FormStartPosition.CenterScreen;
            //frm_M007_GOODS.ShowDialog("",txtGOODS_NM.Text.Trim());
            //if (strGoodID.Trim().Length > 1)
            //{
            //    AddRowToGird(strGoodID, strGoodNM);
            //}
        }

        #endregion

        #region 事件

        private void M009_STORAGE_INPUT_Load(object sender, EventArgs e)
        {
            blFormLoad = true;
            InitProcess();
            txtGOODS_NM.Focus();
        }
      
        private void grdmentTempBillInfo_ButtonClick(object sender, DataGridViewCellEventArgs e)
        {
            int intColumnIndex = e.ColumnIndex;
            if (intColumnIndex == 7 && e.RowIndex != -1)
            {
                grdmentTempBillInfo.Rows.RemoveAt(
                    grdmentTempBillInfo.CurrentRow.Index);
                grpTempBillInfo.Text = strGroupTitle + "  共" + grdmentTempBillInfo.Rows.Count.ToString() + "件";
            }
            if (0 == grdmentTempBillInfo.Rows.Count)
            {
                grpTempBillInfo.Text = strGroupTitle;
            }
        }      
       
        protected override void AllTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            base.AllTextBox_KeyDown(sender, e);
        }

        private void AllTextBox_KeyPrecess(object sender, KeyPressEventArgs e)
        {
            string strValue = ((TextBox)sender).Text.Trim();
            switch (((TextBox)sender).Name)
            {
                case "txtGOODS_ID":
                    FormBase.UKeyPress(e, InputType.Num);
                    break;
                case "txtBUSINESS_TIME":
                    FormBase.UKeyPress(e, InputType.Date);
                    break;
            }
        }

        private void AllTextBox_GotFocus(object sender, EventArgs e)
        {
            string strValue = ((TextBox)sender).Text.Trim();
            switch (((TextBox)sender).Name)
            {
                case "txtGOODS_ID":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Disable;
                    break;
                case "txtGOODS_NM":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Hiragana;
                    break;
                case "txtUNITS":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Disable;
                    ((TextBox)sender).Text =
                        ((TextBox)sender).Text.Replace(",", "");
                    break;
                case "txtBUSINESS_TIME":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Disable;
                    ((TextBox)sender).Text =
                        ((TextBox)sender).Text.Replace("/", "");
                    break;
            }
        }

        private void AllTextBox_LostFocus(object sender, EventArgs e)
        {
            string strValue = ((TextBox)sender).Text.Trim().Replace("/","");
            strValue = strValue.Replace(",", "");
            switch (((TextBox)sender).Name)
            {
                case "txtUNITS":
                    ((TextBox)sender).Text =
                        NumPriceFormat(strValue);                        
                    break;
                case "txtBUSINESS_TIME":
                    ((TextBox)sender).Text =
                        DateFormat(strValue);
                    break;
            }
        }

        private void AllButton_Click(object sender, EventArgs e)
        {
            if (Cursor.Current == Cursors.WaitCursor)
            {
                return;
            }
            Cursor.Current = Cursors.WaitCursor;

            string strButtonName = ((Button)sender).Name;

            switch (strButtonName)
            {
                #region btnGoodsIDSearch

                case "btnGoodsIDSearch":
                    {
                        SearchGoods();
                    }
                    break;

                #endregion

                #region btnAdd

                case "btnAdd":
                    if (CheckProcessLoop(enumOperation.Add) == false)
                    {
                        Cursor.Current = Cursors.Default;
                        return;
                    }
                    if (0 == grdmentTempBillInfo.Rows.Count)
                    {
                        Method.MessageShow("W11045", dataName);//没有要入库的商品！
                        txtGOODS_NM.Focus();
                        Cursor.Current = Cursors.Default;
                        return;
                    }
                    
                    string strReturn = string.Empty;
                    string strUnits=string.Empty;
                    string strBuyingPrice=string.Empty;
                    string strProduceDate = string.Empty;
                    string strOverProduceDate=string.Empty;
                    bool blTemp = false;
                    List<MODEL.MOD_M009_STORAGE_INPUT> Storage = new List<XiaoGang.Soft.XGWMS.MODEL.MOD_M009_STORAGE_INPUT>();
                    for (int i = 0; i < grdmentTempBillInfo.Rows.Count; i++)
                    {
                        MODEL.MOD_M009_STORAGE_INPUT modelStorage = new XiaoGang.Soft.XGWMS.MODEL.MOD_M009_STORAGE_INPUT();
                        modelStorage.UPDATER_ID = GlobalVal.gloUserInfo.USER_ID.Trim();
                        modelStorage.TERMINAL_CD = GlobalVal.gloUserInfo.TERMINAL_CD.Trim();
                        modelStorage.STORAGE_ID = "";//采番
                        modelStorage.BILL_ID = "";
                        modelStorage.STOREROOM_ID = cmbSTOREROOM_ID.SelectedValue.ToString();
                        modelStorage.GOODS_ID = grdmentTempBillInfo.Rows[i].Cells["GOODS_ID"].Value.ToString();
                        strUnits = grdmentTempBillInfo.Rows[i].Cells["UNITS"].Value.ToString().Trim();
                        strBuyingPrice = grdmentTempBillInfo.Rows[i].Cells["BUYING_PRICE"].Value.ToString().Trim();
                        strProduceDate = grdmentTempBillInfo.Rows[i].Cells["PRODUCE_DATE"].Value.ToString().Replace("/", "");
                        strOverProduceDate = grdmentTempBillInfo.Rows[i].Cells["OVER_PRODUCE_DATE"].Value.ToString().Replace("/", "");
                        if (!CheckUnitsBuyingPriceDate(strUnits,strBuyingPrice,strProduceDate,strOverProduceDate,out strReturn))
                        {
                            Method.MessageShow(strReturn,dataName);
                            return;
                        }
                        modelStorage.UNITS = Int32.Parse(strUnits);
                        modelStorage.PRODUCE_DATE = strProduceDate;
                        modelStorage.OVER_PRODUCE_DATE = strOverProduceDate;
                        modelStorage.BUYING_PRICE = strBuyingPrice != "" ? decimal.Parse(strBuyingPrice) : -1m;
                        modelStorage.SELLING_PRICE = -1m;
                        blTemp = bool.Parse(grdmentTempBillInfo.Rows[i].Cells["Red_Black"].Value.ToString());
                        if (blTemp)
                        {
                            modelStorage.RED_BLACK = "1";
                        }
                        else
                        {
                            modelStorage.RED_BLACK = "0";
                        }
                        modelStorage.MEMO = grdmentTempBillInfo.Rows[i].Cells["MEMO"].Value.ToString();
                        Storage.Add(modelStorage);
                    }
                    if (Method.MessageShow("Q12009", dataName) == DialogResult.Yes)
                    {
                        bool blFlage = InPutStorage(Storage, out strReturn);
                        if (!blFlage)
                        {
                            Method.MessageShow(strReturn,dataName);
                            Cursor.Current = Cursors.Default;
                            return;
                        }
                        grdmentTempBillInfo.Rows.Clear();
                        grpTempBillInfo.Text = strGroupTitle;
                        ClearControl();
                        txtGOODS_NM.Focus();
                    }
                    
                    break;

                #endregion 入库
            }
            Cursor.Current = Cursors.Default;
        }

        private void cmbSTOREROOM_ID_GotFocus(object sender, EventArgs e)
        {            
            cmbSTOREROOM_ID.SelectAll();
        }        

        private void dtpBUSINESS_TIME_CloseUp(object sender, EventArgs e)
        {
            txtBUSINESS_TIME.Text = Method.TimeFormat( dtpBUSINESS_TIME.Value.ToString("yyyyMMdd"));
        }

        private void dtpPRODUCE_DATE_KeyDown(object sender, KeyEventArgs e)
        {
            base.AllTextBox_KeyDown(sender, e);
        }

        private void dtpOVER_PRODUCE_DATE_KeyDown(object sender, KeyEventArgs e)
        {
            base.AllTextBox_KeyDown(sender, e);
        }        

        private void M009_STORAGE_INPUT_Activated(object sender, EventArgs e)
        {
            if (!blFormLoad)
            {
                string strValueStoreRoom = cmbSTOREROOM_ID.SelectedValue.ToString();
                Method.CmbDataBound("STOREROOM_ID", cmbSTOREROOM_ID);
                foreach (System.Data.DataRowView drStore in this.cmbSTOREROOM_ID.Items)
                {
                    if (strValueStoreRoom == drStore["STOREROOM_ID"].ToString().Trim())
                    {
                        cmbSTOREROOM_ID.SelectedValue = strValueStoreRoom;
                        return;
                    }
                }
                cmbSTOREROOM_ID.SelectedIndex = 0;
            }
            blFormLoad = false;
        }

        private void txtGOODS_NM_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                SearchGoods();
                txtGOODS_NM.Focus();
                txtGOODS_NM.SelectAll();
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                isEnter = true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void grdmentTempBillInfo_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
        {
            if (isEnter)
            {
                string strUNITS = grdmentTempBillInfo.CurrentRow.Cells["UNITS"].EditedFormattedValue.ToString().Trim();
                string strPRODUCE_DATE = grdmentTempBillInfo.CurrentRow.Cells["PRODUCE_DATE"].EditedFormattedValue.ToString().Trim();
                string strOVER_PRODUCE_DATE = grdmentTempBillInfo.CurrentRow.Cells["OVER_PRODUCE_DATE"].EditedFormattedValue.ToString().Trim();
                string strBUYING_PRICE = grdmentTempBillInfo.CurrentRow.Cells["BUYING_PRICE"].EditedFormattedValue.ToString().Trim();
                if (strUNITS != "")//Check商品数量
                {   
                    int UNITS = 0;
                    if (!Method.CheckIsOnlyNum(strUNITS))
                    {
                        Method.MessageShow("W11039", dataName);//商品数量必须为正整数！
                        grdmentTempBillInfo.BeginEdit(true);
                        grdmentTempBillInfo.CurrentRow.Cells["UNITS"].Selected = true;
                        //grdmentTempBillInfo.CurrentRow.Cells["UNITS"].EditType=DataGridView.
                        return;
                    }
                    UNITS = Convert.ToInt32(strUNITS);
                    if (UNITS <= 0)
                    {
                        Method.MessageShow("W11039", dataName);//商品数量必须为正整数！
                        return;
                    }
                }
                if (strBUYING_PRICE != "")//Check 进价
                {

                    if (!Method.CheckIsNumerc(strBUYING_PRICE))
                    {
                        Method.MessageShow("W11041", dataName);//进价必须为数字！
                        return;
                    }
                    decimal BUYING_PRICE = decimal.Parse(strBUYING_PRICE);
                    if (BUYING_PRICE <= 0)
                    {
                        Method.MessageShow("W11042", dataName);//进价必须大于0！
                        return;
                    }
                }
                if (strPRODUCE_DATE != "")//Check 生产日期
                {
                    if (!Method.CheckIsDate(strPRODUCE_DATE))
                    {
                        Method.MessageShow("W11043", dataName);//生产日期不是合理的日期格式！
                        return;
                    }
                }
                if (strOVER_PRODUCE_DATE != "")//Check 过期日期
                {
                    if (!Method.CheckIsDate(strOVER_PRODUCE_DATE))
                    {
                        Method.MessageShow("W11044", dataName);//过期日期不是合理的日期格式！
                        return;
                    }
                }
                grdmentTempBillInfo.CancelEdit();
            }
        }

        private bool CheckUnitsBuyingPriceDate(string strUnits,string strBuyingPrice,string strProduceDate,string strOverProduceDate,out string strMessage)
        {
            if (strUnits != "")//Check商品数量
            {   
                int UNITS = 0;
                if (!Method.CheckIsOnlyNum(strUnits))
                {
                    strMessage="W11039";//商品数量必须为正整数！
                    return false;
                }
                UNITS = Convert.ToInt32(strUnits);
                if (UNITS <= 0)
                {
                    strMessage="W11039";//商品数量必须为正整数！
                    return false;
                }
            }
            else
            {
                strMessage="W11046";//商品数量不能为空！
                return false;
            }

            if (strBuyingPrice != "")//Check 进价
            {
                if (!Method.CheckIsNumerc(strBuyingPrice))
                {
                    strMessage = "W11041";//进价必须为数字！
                    return false;
                }
                decimal BUYING_PRICE = decimal.Parse(strBuyingPrice);
                if (BUYING_PRICE <= 0)
                {
                    strMessage = "W11042";//进价必须大于0！
                    return false;
                }
            }

            if (strProduceDate != "")//Check 生产日期
            {
                if (!Method.CheckIsDate(strProduceDate))
                {
                    strMessage = "W11043";//生产日期不是合理的日期格式！
                    return false;
                }
            }

            if (strOverProduceDate != "")//Check 过期日期
            {
                if (!Method.CheckIsDate(strOverProduceDate))
                {
                    strMessage="W11044";//过期日期不是合理的日期格式！
                    return false;
                }
            }
            strMessage = "";
            return true;
        }

        private void M009_STORAGE_INPUT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString().Trim() == "")
            {
                txtGOODS_No.Text = "";
                txtGOODS_No.Focus();
            }
        }

        private void txtGOODS_ID_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                if (txtGOODS_No.Text.Trim().Length > 0)
                {
                    //查找商品                    
                    XiaoGang.Soft.XGWMS.BLL.BLL_M007_GOODS bll_M007_GOODS = new XiaoGang.Soft.XGWMS.BLL.BLL_M007_GOODS();
                    ReturnValue rvReturnValue = bll_M007_GOODS.GetSingleDataByNo(txtGOODS_No.Text.Trim());
                    if (rvReturnValue.IsSuccess == false)
                    {
                        Method.MessageShow(rvReturnValue.ErrorID,dataName);
                        return;
                    }
                    if (rvReturnValue.ResultDataSet.Tables[0].Rows.Count == 1)
                    {
                        AddRowToGird(rvReturnValue.ResultDataSet.Tables[0].Rows[0]["GOODS_ID"].ToString().Trim(), rvReturnValue.ResultDataSet.Tables[0].Rows[0]["GOODS_NM1"].ToString().Trim());
                    }
                    else if (rvReturnValue.ResultDataSet.Tables[0].Rows.Count > 1)
                    {
                        if (GlobalVal.gloSearchGoodsForm == null)
                        {
                            GlobalVal.gloSearchGoodsForm = new M007_GOODS(true);
                        }
                        GlobalVal.gloSearchGoodsForm.ShowSearchGoodsDialog(this, txtGOODS_No.Text.Trim(), "", txtGOODS_NM.Text.Trim());
                        if (GlobalVal.gloSelectedGoods.ItemCount > 0)
                        {
                            AddRowToGird(GlobalVal.gloSelectedGoods.SelectedItems[0].GOODS_ID
                                , GlobalVal.gloSelectedGoods.SelectedItems[0].GOODS_NM1);
                            txtGOODS_NM.Focus();
                        }

                        //strGoodID = "";//商品编号
                        //strGoodNM = "";//商品名称
                        //M007_GOODS frm_M007_GOODS = new M007_GOODS(true, "M009_STORAGE_INPUT");
                        //frm_M007_GOODS.StartPosition = FormStartPosition.CenterScreen;
                        //frm_M007_GOODS.ShowDialog(strGoodsNO, txtGOODS_NM.Text.Trim());
                        //if (strGoodID.Trim().Length > 1)
                        //{
                        //    AddRowToGird(strGoodID, strGoodNM);
                        //    txtGOODS_NM.Focus();
                        //}
                    }
                }
            }
        }

        #endregion
    }
}